//
//  WYStatusModel.swift
//  weibo
//
//  Created by 王玉东 on 16/4/13.
//  Copyright © 2016年 wyd. All rights reserved.
//

import UIKit

class WYStatusModel: NSObject {
    
    var status:WYUserStatues?
    
    var mbImage:UIImage? //认证图片
    
    var verifiedImage:UIImage? //在初始化时候init 方法中赋值,以后在调用就会在内存中调用
    
    var respostTitle:String?
    
    var comentTitle:String?
    
    var ohYeahTitle:String?
    
    var source: String?
    //var time: String?

    
    var headImageURL:NSURL? {
    
        return NSURL(string: status?.user?.profile_image_url ?? "")
        
    }
        var time: String? {
    
        return NSDate.sinaDate(status?.created_at ?? "")?.fullDescription()
        }
    
    init(status: WYUserStatues) {
        super.init()
        
        
        dealWeiboSource()
       
        self.status = status
        
        respostTitle = dealToolbarText(status.reposts_count ?? 0, defaultTitle: "转发")
        respostTitle = dealToolbarText(status.coments_count ?? 0, defaultTitle: "评论")
        
        respostTitle = dealToolbarText(status.attitudes_count ?? 0, defaultTitle: "赞")
        
        
        
        let mbType = status.user?.mbrank ??  0
        if mbType > 0 && mbType < 7 {
            mbImage = UIImage(named: "common_icon_membership_level\(mbType)")
        
        }
        
       // let verifiedType = self.status?.user?.verified_type ?? 0
        let verifiedType = status.user?.verified ?? -1
        switch verifiedType {
        case 1:
            verifiedImage = UIImage(named: "avatar_vip")
//        case 2,3,5:
//            verifiedImage = UIImage(named: "avatar_enterprise_vip")
//        case 220:
//            verifiedImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedImage = nil
        }
    // dealSinaDate()
    }
    
}
extension WYStatusModel{
    
    private func dealWeiboSource() {
        let startFlag = "\">"
        let endFlag = "</"
        let str = status?.source ?? ""
        if let startRange = str.rangeOfString(startFlag), endRange = str.rangeOfString(endFlag) {
            let range = startRange.endIndex..<endRange.startIndex
            source = str.substringWithRange(range)
        }
    }

    private func dealToolbarText(count: Int, defaultTitle: String) ->String {
    
    //let count = 12000
        if count <= 0{
        
    return defaultTitle
        }
        if count > 10000 {
        
        return "\((Double(count/1000) / 10))万"
        }
    
    return "\(count)"
    }
//    private func dealSinaDate() {
//        //Thu Apr 14 11:00:10 +0800 2016
//        let df = NSDateFormatter()
//        //指定格式
//        df.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
//        //指定区域 不指定 在模拟器上 可以正常运行  但是 在真机会崩溃
//        df.locale = NSLocale(localeIdentifier: "en")
//        //将对应的字符串转换为 NSDate
//        let sinaDate = df.dateFromString(status?.created_at ?? "")
//        
//        //将日期对象转换为 指定格式的字符串
//        df.dateFormat = "yyyy-MM-dd HH:mm:ss"
//        time = df.stringFromDate(sinaDate!)
//    }
//    private func dealmbRank() {
//        let mbType = status?.user?.mbrank ?? 0
//        if mbType > 0 && mbType < 7 {
//            mbImage = UIImage(named: "common_icon_membership_level\(mbType)")
//        }
//    }
//    
//    private func dealVerifiedType() {
//        //用户认证类型
//        let verifiedType = status?.user?.verified ?? -1
//        switch verifiedType {
//        case 1:
//            verifiedImage = UIImage(named: "avatar_vip")
//        case 2,3,5:
//            verifiedImage = UIImage(named: "avatar_enterprise_vip")
//        case 220:
//            verifiedImage = UIImage(named: "avatar_grassroot")
//        default:
//            verifiedImage = nil
//        }
//        
//    }


}